package hikari.pathfind.data;

import hikari.exception.ValidationException;
import hikari.pathfind.data.editor.Cell;
import hikari.pathfind.data.editor.PathFindParams;
import hikari.pathfind.data.editor.PolygonConnection;
import java.awt.Point;
import java.util.HashMap;
import java.util.Map;

/**
 * @author Dmitry
 */
public class CellData {
    private Cell[][] cells;
    private int xCount;
    private int yCount;
    private double x;
    private double y;
    private double width;
    private double height;
    private Map<CellData, PolygonConnection> connections = new HashMap<>();

    public void setConnections(Map<CellData, PolygonConnection> connections) {
        this.connections = connections;
    }

    public void clearConections() {
        connections = new HashMap<>();
    }

    public Map<CellData, PolygonConnection> getConnection() {
        return connections;
    }

    public void addConnectedPolygon(CellData parea, PolygonConnection connection) {
        if (connections == null) {
            connections = new HashMap<>();
        }
        connection.setPolygon(parea);
        connections.put(parea, connection);
    }

    public void createCells() {
        cells = new Cell[xCount][yCount];
        for (int j = 0; j < yCount; j++) {
            for (int i = 0; i < xCount; i++) {
                cells[i][j] = new Cell();
            }
        }
    }

    public boolean getIndexOfCell(double x, double y, Point outputPoint) {
        if (x < this.x || x >= this.x + width || y < this.y || y >= this.y + height) {
            return false;
        }

        int xIndex = (int) ((x - this.x) / PathFindParams.CELL_SIZE);
        int yIndex = (int) ((y - this.y) / PathFindParams.CELL_SIZE);
        outputPoint.x = xIndex;
        outputPoint.y = yIndex;
        return true;
    }

    /**
     * By Coordinates
     */
    public boolean isActiveCell(double x, double y) {
        if (x < this.x || x >= this.x + width || y < this.y || y >= this.y + height) {
            return false;
        }

        int xIndex = (int) ((x - this.x) / PathFindParams.CELL_SIZE);
        int yIndex = (int) ((y - this.y) / PathFindParams.CELL_SIZE);
        return cells[xIndex][yIndex].isActive();
    }

    public Cell getCell(int x, int y) {
        if ((x < 0 || x >= xCount) || (y < 0 || y >= yCount)) {
            throw new ValidationException(String.format("X and Y cannot be more than [%d,%d] but found [%d,%d]", xCount, yCount, x, y));
        }
        return cells[x][y];
    }

    public int getxCount() {
        return xCount;
    }

    public void setxCount(int xCount) {
        this.xCount = xCount;
    }

    public int getyCount() {
        return yCount;
    }

    public void setyCount(int yCount) {
        this.yCount = yCount;
    }

    public double getX() {
        return x;
    }

    public void setX(double x) {
        this.x = x;
    }

    public double getY() {
        return y;
    }

    public void setY(double y) {
        this.y = y;
    }

    public double getWidth() {
        return width;
    }

    public void setWidth(double width) {
        this.width = width;
    }

    public double getHeight() {
        return height;
    }

    public void setHeight(double height) {
        this.height = height;
    }
}
